/**
 * @file .
 * @author wangxiaowei <wangxiaowei0303@163.com>
 * @date 2015-05-28 01:41
 * HistUtil 类的测试程序
 */

#include "HistUtil.h"

#include <math.h>
#include <stdio.h>

#include <string>
#include <algorithm>

#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>


using namespace cv;

// 当程序缺少必需的参数时,调用该函数打印帮助信息.
void 
help(const char *arg0)
{
    printf("%s;实现了直方图均衡算法.\n",arg0);
    printf("使用帮助; %s 图像名\n",arg0);
}

// 将 depth 由枚举值转换为字符串,如 CV_8U 转换为 "CV_8U".
const char*
depth2str(int depth)
{
    const char *depthStr;
    switch (depth) {
    case CV_8U: depthStr = "CV_8U"; break;
    case CV_8S: depthStr = "CV_8S"; break;
    case CV_16U: depthStr = "CV_16U"; break;
    case CV_16S: depthStr = "CV_16S"; break;
    case CV_32S: depthStr = "CV_32S"; break;
    case CV_32F: depthStr = "CV_32F"; break;
    case CV_64F: depthStr = "CV_64F"; break;
    default: depthStr = "ERR";
    }
    return depthStr;
}

int
main(int argc,char *argv[])
{
    if (argc < 2) {
        fputs("ERROR;缺少必需的参数\n",stderr);
        help(argv[0]);
        return 1;
    }

    Mat img = imread(argv[1],IMREAD_GRAYSCALE);
    if (img.data == 0) {
        fprintf(stderr,"ERROR;不支持的图像文件 %s\n",argv[1]);
        return 2;
    }
    
    printf("INFO;depth: %s;channels: %d\n",depth2str(img.depth()),img.channels());
    printf("INFO;width: %d;height: %d\n",img.cols,img.rows);

    unsigned int srchist[256];//原图像的直方图.
    HistUtil::calc(img,srchist);
    Mat cvdst; //opencv 提供的直方图均衡化算法后的结果.
    unsigned int cvdsthist[256];
    equalizeHist(img,cvdst);
    HistUtil::calc(cvdst,cvdsthist);
    std::string name(argv[1]);
    imshow(name + "-cv",cvdst);
    imshow(name,img);
    imshow(name + "-直方图",HistUtil::draw(srchist));
    imshow(name + "-cv-直方图",HistUtil::draw(cvdsthist));
    waitKey();
    return 0;
}

